<?php
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
require_once(dirname(__FILE__) . "/../../dbcom/RagionCom.php");
require_once(dirname(__FILE__) . "/../../dbcom/MachineCom.php");
require_once(dirname(__FILE__) . "/../../dbcom/UserCom.php");
require_once(dirname(__FILE__) . "/../../common/Common.php");

$ragioncom = new RagionCom();
$machinecom = new MachineCom();
$usercom = new UserCom();
$common = new Common();
//$communityarr = $ragioncom->getRagionByType(2);

$userid = $common->getLogin();
if (!isset($userid) || empty($userid)) {
    header('Location:./../login.php');
}

$user = $usercom->getUserById($userid);
if ($user['role'] != 0) {
    $mechine = $machinecom->getwMechine($user['community_id']);
} else {
    $mechine = $machinecom->getMechine();
}

$mymechine = array();
$statusarr = "";
$data = "[";
if (!empty($mechine)) {
    $data = "[";
    foreach ($mechine as $k => $v) {
        $mymechine[$k]['id'] = $v['id'];
        $mymechine[$k]['des'] = $v['des'];
        $mymechine[$k]['longitude'] = $v['longitude'];
        $mymechine[$k]['latitude'] = $v['latitude'];
        $community = $ragioncom->getRagionById($v['community_id']);
        $mymechine[$k]['community'] = $community['name'];
        $data .= '[' . $v['longitude'] . "," . $v['latitude'] . "," . '"备注: ' . $v['des'] . '<br>' . '社区: ' . $community['name'] . '"],';
        $statusarr .= $v['status'] . ",";
    }
    $data = substr($data, 0, strlen($data) - 1);
    $statusarr = substr($statusarr, 0, strlen($statusarr) - 1);
}
$data .= "]";
//echo $data;
//var_dump($mymechine);
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        <meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
        <style type="text/css">
            body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
        </style>
        <link href="../public/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
        <link href="../public/css/font-awesome.css?v=4.4.0" rel="stylesheet">
        <link href="../public/css/animate.css" rel="stylesheet">
        <link href="../public/css/style.css?v=4.1.0" rel="stylesheet">
        <script src="../public/js/jquery.min.js?v=2.1.4"></script>
        <script src="../public/js/bootstrap.min.js?v=3.3.6"></script>
        <script src="../public/js/plugins/layer/layer.min.js"></script>
        <script type="text/javascript" src="https://api.map.baidu.com/api?v=2.0&ak=tXfVciWna9Y5Wni6D37u5qkvYAc4rNWG"></script>
        <title>爬楼机地图</title>
    </head>
    <body>
        <div id="allmap" style="width:100%;float:left;"></div>>
    </body>
</html>
<script type="text/javascript">
    // 百度地图API功能	
    map = new BMap.Map("allmap");
    map.centerAndZoom(new BMap.Point(120.58531, 31.298886), 13);
    var data_info = <?= $data ?>;
    var arr = new Array(<?= $statusarr ?>);
    //console.log(arr);
    map.setCurrentCity("苏州");          // 设置地图显示的城市 此项是必须设置的
    map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
    var opts = {
        width: 250, // 信息窗口宽度
        height: 80, // 信息窗口高度
        title: "机器备注及所属社区", // 信息窗口标题
        enableMessage: true//设置允许信息窗发送短息
    };
    console.log(data_info);
    for (var i = 0; i < data_info.length; i++) {
        //创建图标
        if (arr[i] == 1) {
            console.log(arr[i]);
            var myIcon = new BMap.Icon("../public/img/ricon.png", new BMap.Size(20, 27));
            var marker = new BMap.Marker(new BMap.Point(data_info[i][0], data_info[i][1]), {icon: myIcon});  // 创建标注

        } else {
            console.log(arr[i]);
            var myIcon1 = new BMap.Icon("../public/img/blackicon.png", new BMap.Size(20, 27));
            var marker = new BMap.Marker(new BMap.Point(data_info[i][0], data_info[i][1]), {icon: myIcon1});  // 创建标注
        }
        //var marker2 = new BMap.Marker(pt,{icon:myIcon}); 
        var content = data_info[i][2];
        map.addOverlay(marker);               // 将标注添加到地图中
        addClickHandler(content, marker);
    }
    function addClickHandler(content, marker) {
        marker.addEventListener("click", function (e) {
            openInfo(content, e)
        }
        );
    }
    function openInfo(content, e) {
        var p = e.target;
        var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
        var infoWindow = new BMap.InfoWindow(content, opts);  // 创建信息窗口对象 
        map.openInfoWindow(infoWindow, point); //开启信息窗口
    }
</script>